# -*- coding: utf-8 -*-
from sklearn.datasets import load_iris
from sklearn.feature_selection import mutual_info_classif
from sklearn.feature_selection import SelectKBest, SelectFromModel
from sklearn.model_selection import train_test_split

from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report


import numpy as np
from matplotlib import pyplot as plt


iris = load_iris()
x = iris.data
y = iris.target
print(x.shape)

x_new = SelectKBest(mutual_info_classif, k=3).fit_transform(x, y)
print(x_new.shape)


clf = DecisionTreeClassifier().fit(x,y)
model = SelectFromModel(clf, prefit=True)
x_new = model.transform(x)
print(x_new.shape)
print(clf.feature_importances_) 

#划分训练集与验证集
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=0)

#训练决策树
clf2 = DecisionTreeClassifier(max_leaf_nodes=3, random_state=0)
clf2 = clf2.fit(X_train, y_train)

#预测结果
y_predict = clf2.predict(X_test)

#答应模型的验证报告
print(classification_report(y_test, y_predict))

from sklearn.model_selection import cross_val_score

clf3 = DecisionTreeClassifier(max_leaf_nodes=3, random_state=0)
print(cross_val_score(clf3, x, y, cv=5))

from sklearn.tree import plot_tree

plot_tree(clf2)
plt.savefig("test_tree.png")

from sklearn.naive_bayes import GaussianNB

gnb = GaussianNB()

y_predict2 = gnb.fit(X_train,y_train).predict(X_test)

print(classification_report(y_test, y_predict2))